<template>
  <div>
    <!-- 面包屑导航 separator是以>分割 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面板 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="rolelist" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 最外的一行 -->
            <el-row v-for="first in scope.row.children" class="vcenter bdtop">
              <!-- 一级的6 -->
              <el-col :span="6">
                <el-tag closable>{{first.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级的18 -->
              <el-col :span="18">
                <!-- 二级三级范围 -->
                <el-row v-for="second in first.children" class="vcenter bdtop">
                  <!-- 左边二级 -->
                  <el-col :span="6">
                    <el-tag closable type="success">{{second.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 右边三级 -->
                  <el-col :span="18">
                    <el-tag
                      @close="delright(scope.row,third.id)"
                      closable
                      type="warning"
                      v-for="third in second.children"
                    >{{third.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column prop="level" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              @click="showrightdialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 点击分配角色发送ajax，获取所有权限，并且弹出分配权限的对话框 -->
    <el-dialog title="分配权限" :visible.sync="dialogVisible" width="80%" @close="setrightdialog">
      <!-- 树形控件 -->
      <el-tree
        :data="rightlist"
        show-checkbox
        :props="treeProps"
        node-key="id"
        default-expand-all
        :default-checked-keys="defkeys"
        ref="treeref"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rolelist: [],
      roleId: "",
      dialogVisible: false,
      rightlist: [], //权限树形数据
      treeProps: {
        label: "authName", //显示的名字
        children: "children" //对应的孩子的单词
      },
      //   拿到所有三级权限的id放到defkeys里
      defkeys: [] //要勾选的id
    };
  },
  created() {
    this.getrolelist();
  },
  methods: {
    setrightdialog() {
      this.defkeys = []; //重置为空
    },
    // 点击确定，获取所选中的权限id 在分配权限就先提前存上role的id发ajax去添加
    //this.$refs.treeref.getCheckedKeys()完全选中的id数组
    //this.$refs.treeref.getHalfCheckedKeys()半选中的id数组
    async allotRights() {
      const keys = [
        ...this.$refs.treeref.getCheckedKeys(),
        ...this.$refs.treeref.getHalfCheckedKeys()
      ];
      const idstr = keys.join(",");
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idstr }
      );
      if (res.meta.status != 200) {
        return this.$message.error("分配权限失败");
      }
      this.$message.success("分配权限成功");
      this.getrolelist();
      this.dialogVisible = false;
    },
    getleafkeys(node, defkeys) {
      if (!node.children) {
        return defkeys.push(node.id);
      }
      // 一级二级都要循环
      node.children.forEach(item => {
        this.getleafkeys(item, defkeys);
      });
    },
    //   显分配权限对话框
    async showrightdialog(role) {
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status != 200) {
        return this.$message.error("获取权限失败");
      }
      //   console.log(role);
      this.roleId = role.id;
      this.rightlist = res.data;
      this.dialogVisible = true; //显示对话框
      this.getleafkeys(role, this.defkeys); //调用它，把勾选的id放到defkeys
    },
    //   删除权限
    async delright(role, id) {
      let result = await this.$confirm(
        "此操作将永久删除该权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      // reult值为confirm时就是确认删除，cancel就是取消
      if (result != "confirm") return this.$message.info("取消删除成功");
      //   console.log("确定删除");
      let { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${id}`
      );
      console.log(res);
      if (res.meta.status != 200) return this.$message.error("删除失败");
      // role.children对应所有权限
      role.children = res.data; //把返回的最新所有权限赋值给role。children
    },
    //   获取角色页面
    async getrolelist() {
      let { data: res } = await this.$http.get("roles");
      if (res.meta.status != 200) {
        return this.$message.error("获取角色失败");
      }
      this.rolelist = res.data;
      //   console.log(res);
    }
  }
};
</script>
<style lang="less" scoped>
.el-card {
  margin-top: 10px;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border: 1px solid #333;
  margin-right: -1px;
  margin-bottom: -1px;
  margin-top: -1px;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>


